from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse,HttpResponseRedirect

from . import models
from django.contrib import auth
from django.urls import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


# def index(request):
#     return HttpResponse('hello world!--zoutao')
#可以使用面向对象的方式来写，声明为class类来写函数

#使用render渲染(显示)html页面,参数是字典，显示到前端
#实现分页了已经
def index(request):
    articles = models.Article.objects.all()  #获取全部的Article对象
    paginator = Paginator(articles, 10) #每页显示三个
    page = request.GET.get('page')
    # first_page = paginator.page(1)
    # last_page = paginator.page(paginator.num_pages)
    try :
        post_list = paginator.page(page)# If page is not an integer, deliver first page.
    except PageNotAnInteger :
        post_list = paginator.page(1)#取对象的第一分页对象
    except EmptyPage :
        post_list = paginator.page(paginator.num_pages)# If page is out of range (e.g. 9999), deliver last page of results.
    return render(request, 'blog/index.html', {'post_list' : post_list})



#使用render渲染(显示)html页面,参数是字典，显示到前端,先获取到需要的数据，选择性返回
def article_page(request,article_id):
    article = models.Article.objects.get(pk=article_id)
    return render(request, 'blog/article_page.html', {'article': article})


#使用render渲染(显示)html页面,参数是字典，显示到前端,先获取到需要的数据，选择性返回
def article_edit_page(request,article_id):
    #如果id是0，是新文章，如不是，传递id
    if str(article_id) == '0':
        return render(request,'blog/article_edit_page.html')
    article = models.Article.objects.get(pk=article_id)
    return render(request, 'blog/article_edit_page.html',{'article':article})


#action提交数据来了做响应
def article_edit_page_action(request):
    title = request.POST.get('title','默认标题')
    content = request.POST.get('content','默认内容')
    #获取隐藏域传递来的参数，默认的是0
    article_id = request.POST.get('article_id_hidden','0')

    if article_id == '0':
        #新建博客文章数据
        models.Article.objects.create(title=title, content=content)
        #保存了以后需要回显
        articles = models.Article.objects.all()
        return render(request,'blog/index.html',{'articles':articles})
#编辑原来文章的情况,以赋值更新
    article = models.Article.objects.get(pk=article_id)
    article.title = title
    article.content = content
    article.save()
    return render(request,'blog/article_page.html',{'article':article})



# #定义表单模型
# class UserForm(forms.Form):
#     username = forms.CharField(label='用户名：',max_length=100)
#     password = forms.CharField(label='密码：',widget=forms.PasswordInput())

# #登录
# def login(request):
#     if request.method == 'POST':
#         uf = UserForm(request.POST)
#         if uf.is_valid():
#             #获取表单用户密码
#             username = uf.cleaned_data['username']
#             password = uf.cleaned_data['password']
#             #获取的表单数据与数据库进行比较
#             user = User.objects.filter(username__exact = username,password__exact = password)
#             if user:
#                 return render_to_response('success.html',{'username':username})
#             else:
#                 return HttpResponseRedirect('/login/')
#     else:
#         uf = UserForm()
#     return render_to_response('login.html',{'uf':uf})

from django.contrib.auth import authenticate

def login(request):
    username = request.POST.get('username',)
    password = request.POST.get('password',)
    user =auth.authenticate(username = 'zoutao',password='123456')
    if user is not None and user.is_active:
        # if user.is_active:
        print('登录成功！')
    else:
        print('登录失败！请重试')
        #return HttpResponseRedirect(reverse('blog:index', args=(user,)))

#方法二
# def login(request):
#     username = request.POST.get('username',)
#     password = request.POST.get('password',)
#     user = auth.authenticate(username = 'zoutao',password='123456')
#     if user is not None and user.is_active:
#         # Correct password, and the user is marked "active"
#         auth.login(request, user)
#         # Redirect to a success page.
#         return HttpResponseRedirect(reverse("blog:index",args=(username,)))
#         #return HttpResponseRedirect(reverse('polls:results', args=(question_id,)))
#     else:
#         # Show an error page
#         #return HttpResponseRedirect("blog/index/")
#         return HttpResponseRedirect(reverse("blog:index",args=(username,)))


